MOOC Utils

Utils like auto assignment solver extension and course notice announcment notification for MOOC courses

Application architecture

Introduction#

Next.js App Router layout for the website: root layout, providers (auth, theme, analytics, query), and how pages compose.

Project structure#

Next.js App Router layout:

  • Root layout defines global metadata, fonts, and providers.
  • Pages define route segments and render page-specific content.
  • Shared UI components encapsulate reusable elements like the navigation header and theme toggle.
  • Provider modules configure cross-cutting concerns such as authentication, theming, analytics, and data fetching.

Core components#

  • Root layout and metadata: Defines global metadata, Open Graph, Twitter, and font loading via Next Font.
  • Providers: Composes React Query, authentication context, theme provider, analytics provider, and toast notifications.
  • Authentication context: Manages user session lifecycle, OTP-based sign-in, refresh, and logout.
  • Navigation header: Provides responsive navigation with theme toggle and branding.
  • Global styling: Tailwind-based design tokens with CSS custom properties and dark mode support.
  • Analytics provider: PostHog integration with manual pageview tracking and rewrites for proxying ingestion.

Architecture overview#

The application uses a layered provider pattern:

  • Root layout composes Providers at the root level.
  • Providers wrap the entire app tree with QueryClient, AuthProvider, ThemeProvider, PostHogProvider, and toasts.
  • Pages and components consume context and hooks to access user state, theme, and analytics.

Detailed component analysis#

Root layout and metadata#

  • Metadata configuration includes title template, description, keywords, author/publisher info, robots directives, and Open Graph/Twitter settings.
  • Fonts are loaded using Next Font with variable axes and swap strategy for fast rendering.
  • Root layout renders Providers, NavHeader, and page children inside a single html/body wrapper with font classes applied to the body.

Provider system#

  • QueryClientProvider: Configured with a default staleTime and controlled refetch behavior.
  • AuthProvider: Centralizes user session state, OTP requests, verification, logout, and session refresh.
  • ThemeProvider: Uses next-themes with class-based switching and system preference support.
  • PostHogProvider: Initializes PostHog client and captures pageviews on route changes.
  • Toaster: Provides toast notifications for user feedback.

Authentication context provider#

  • Session initialization: On mount, attempts to load current user profile and sets loading state accordingly.
  • OTP flow: requestOtp triggers backend OTP issuance; verifyOtp exchanges code for session and updates user state.
  • Logout: Clears user state and redirects to login route.
  • Refresh: Periodically refreshes session and updates user state.
  • Hook safety: useAuth enforces usage within AuthProvider.
  • Responsive design: Fixed header with scroll-aware styling, mobile menu toggle, and desktop navigation.
  • Theme toggle: Integrates with next-themes to switch between light, dark, and system modes.
  • Branding and actions: Includes logo, navigation links, and a call-to-action button.

Global styling approach#

  • Tailwind v4 with custom variants and shadcn styling.
  • CSS custom properties define theme tokens mapped to Tailwind variables.
  • Dark mode variables adjust color palettes for contrast and readability.
  • Base layer applies global borders and outlines.

Login and dashboard pages#

  • Login page: Implements a two-step OTP flow with zod validation, loading states, and error messaging. Redirects authenticated users away from the login route.
  • Dashboard page: Protected by AuthGuard, displays user profile, subscriptions, notifications, and logout action.

Analytics provider and rewrites#

  • PostHog client initialized in the browser with environment key and manual pageview capture.
  • Next.js rewrites proxy PostHog ingestion endpoints to external hosts while keeping internal routing clean.

Dependency analysis#

  • Runtime dependencies include Next.js, React, TanStack Query, next-themes, PostHog, Tailwind, and UI primitives.
  • Dev dependencies include ESLint, TypeScript, and Tailwind tooling.
  • The provider stack introduces explicit coupling between auth, theming, analytics, and data fetching.

Performance considerations#

  • Font loading: Next Font with display swap reduces FOIT/FOUT by enabling fast system font fallback during font load.
  • Hydration: Root layout uses suppressHydrationWarning to avoid mismatches when server-rendered HTML toggles theme attributes.
  • Data fetching: QueryClient configured with a short staleTime and disabled refetch on window focus to balance freshness and performance.
  • Analytics: Manual pageview capture avoids redundant automatic tracking and reduces overhead.
  • SSR/CSR: AuthGuard renders a loader while checking authentication state, minimizing hydration jank for protected routes.

Troubleshooting guide#

  • Authentication state not persisting:
  • Verify session cookies are included in fetch requests and backend supports credentials.
  • Check that getMe and refresh endpoints are reachable and return valid user objects.
  • Theme toggle not applying:
  • Ensure next-themes is initialized and attribute is set to class.
  • Confirm CSS custom properties are defined in globals and mapped to Tailwind variables.
  • Analytics events not recorded:
  • Confirm NEXT_PUBLIC_POSTHOG_KEY is set and PostHog client initializes in the browser.
  • Verify rewrites are active and external host is reachable.
  • Hydration warnings:
  • Review root layout and header components for mismatched SSR/CSR content, especially around theme and dynamic content.

Conclusion#

Providers wrap the tree once. Put new cross-cutting concerns there instead of per-page copies.